package cn.yeamin.spring.boot.controller;


import cn.yeamin.spring.boot.pojo.Girl;
import cn.yeamin.spring.boot.service.GirlService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RequestMapping("/girl")
@RestController
public class GirlController {


    @Autowired
    private GirlService girlService;

    @GetMapping("/{id}")
    public Girl findById(@PathVariable("id") Integer id)
    {

        return girlService.getOne(id);
    }

    @GetMapping("/all")
    public List<Girl> findAll()
    {
        return girlService.findAll();
    }

    @GetMapping("/page/{page}/{size}")
    public List<Girl> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size)
    {
        //分页拦截器
        PageHelper.startPage(page, size);
        List<Girl> girls = girlService.findAll();
        PageInfo<Girl> info = new PageInfo<Girl>(girls);
        System.err.println("总记录数: " + info.getTotal());
        System.err.println("当前页最后一条在总记录的行数: " + info.getEndRow());
        return girls;
    }

    @PostMapping("/save")
    public void save(@RequestBody Girl girl) {
        girlService.save(girl);
    }

    @PostMapping("/update")
    public void update(@RequestBody  Girl girl) {
        girlService.update(girl);
    }

    @GetMapping("/del/{id}")
    public void deleteById(@PathVariable("id") Integer id)
    {
        //为了测试缓存,先查再删除
        List<Girl> list1 =  girlService.findAll();
        System.out.println("#######发SQL查数据库#########  " + list1);
        List<Girl> list2 =  girlService.findAll();
        System.out.println("#######从缓存获取#########  " + list2);
        girlService.deleteById(id);
        List<Girl> list3 = girlService.findAll();
        System.out.println("#######删除了缓存从数据库重新获取#########  " + list3);
    }

}
